package com.wang.myapplication.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wang.myapplication.R;
import com.wang.myapplication.tools.SharedPreTool;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class StartupActivity extends AppCompatActivity implements View.OnClickListener, Runnable {

    //private Button mBtnLogin;
    public Handler mHandler;
    private Handler mHandler2;
    private Handler mHandler3;
    public String TAG = "StartupActivity";
    SharedPreferences mSharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startup);
        Boolean isStarted = (Boolean) SharedPreTool.getValue(SharedPreTool.KEY_STARTED, SharedPreTool.BOOLEAN);
        //if (isStarted) {
        //    startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
        //    this.finish();
        //    return;
        //}
        Banner banner = findViewById(R.id.banner);
        List<Integer> images = new ArrayList<>();
        images.add(R.mipmap.a);
        images.add(R.mipmap.b);
        images.add(R.mipmap.c);
        banner.setImages(images).setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        }).isAutoPlay(false).start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(StartupActivity.this, "滴几个"+position, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
                SharedPreTool.putValue(SharedPreTool.KEY_STARTED, true);
                StartupActivity.this.finish();
            }
        });

        new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    //  startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));//这样子肯定错误 ，开启了子线程后还要跟主线程通信啊啊！！
                } catch (InterruptedException pE) {
                    pE.printStackTrace();
                }
            }
        };
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("StartupActivity", "mHandler111--handleMessage里面" + Thread.currentThread());
            }
        };
        mHandler2 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("StartupActivity", "mHandler222--handleMessage里面" + Thread.currentThread());
                //startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
            }
        };
        mHandler3 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Log.e("StartupActivity", "mHandler333--handleMessage里面" + Thread.currentThread());
                // startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
            }
        };
        //开启了子线程
        //new Thread(new Runnable() {
        //    @Override
        //    public void run() {
        //        try {
        //            Thread.sleep(500);
        //            Log.e("StartupActivity", "使用Thread--" + Thread.currentThread());
        //            mHandler2.sendEmptyMessage(0);// mHandler.sendMessageDelayed(msg,3000);//----handle  msg  time
        //        } catch (InterruptedException pE) {
        //            pE.printStackTrace();
        //        }
        //    }
        //}).start();
        //属于主线程启动且运行在主线程  思考为毛
        //this.run();

        //属于主线程启动且运行在主线程  思考为毛
        //new Runnable() {
        //    @Override
        //    public void run() {
        //        try {
        //            Thread.sleep(3000);
        //            Log.e("StartupActivity", "使用匿名实现Runnable--" + Thread.currentThread());
        //            mHandler3.sendEmptyMessage(0);
        //
        //        } catch (InterruptedException pE) {
        //            pE.printStackTrace();
        //        }
        //
        //    }
        //}.run();


        //启动了子线程[看不见]   Runnable只是一个backcall啊所以是主线程  相当于handleMessage
        //mHandler = new Handler();
        //mHandler.postDelayed(new Runnable() {
        //    @Override
        //    public void run() {
        //        //startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
        //        Log.e("StartupActivity", "使用mHandler--postDelayed--" + Thread.currentThread());
        //    }
        //}, 1000);

        // mHandler.sendMessageDelayed(msg,3000);//----handle  msg  time

        //new AsyncTask<Integer, Void, String>() {
        //    @Override
        //    protected String doInBackground(Integer... pIntegers) {
        //        Log.d(""+TAG,"AsyncTask---doInBackground--"+pIntegers.length+pIntegers[0]+""+ Thread.currentThread());
        //        try {
        //            Thread.sleep(3000);
        //        } catch (InterruptedException pE) {
        //            pE.printStackTrace();
        //        }
        //        return null;
        //    }
        //
        //    @Override
        //    protected void onPostExecute(String pS) {
        //        super.onPostExecute(pS);
        //        Log.d(""+TAG,"AsyncTask---onPostExecute--"+pS+ Thread.currentThread());
        //        startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
        //
        //    }
        //}.execute(168);


    }

    @Override
    public void onClick(View pView) {
        startActivity(new Intent(StartupActivity.this, CompanyLoginActivity.class));
        SharedPreTool.putValue(SharedPreTool.KEY_STARTED, true);
    }

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            Log.e("StartupActivity", "使用直接实现Runnable--" + Thread.currentThread());
            mHandler.sendEmptyMessage(0);// mHandler.sendMessageDelayed(msg,3000);//----handle  msg  time

        } catch (InterruptedException pE) {
            pE.printStackTrace();
        }

    }
}

